let arr = [1, 3, 4, 31, 2, 5, 8, 23]
console.log(123);
function mergeSort(arr) {
  if (arr.length < 2) return arr
  let m = Math.floor(arr.length / 2)
  let left = mergeSort(arr.slice(0, m))
  let right = mergeSort(arr.slice(m))
  return merge(left, right)
}

function merge(left, right) {
  let res = []
  let i = 0,
    j = 0
  while (i < left.length && j < right.length) {
    if (left[i] < right[j]) {
      res.push(left[i++])
    } else {
      res.push(right[j++])
    }
  }
  if (i < left.length) {
    res = res.concat(left.slice(i))
  } else {
    res = res.concat(right.slice(j))
  }
  return res
}
const arr1 = mergeSort(arr)
console.log(arr1);